#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::H3C::DisplayFan < Aio::Module::Cmd::H3C
  include Aio::Module

  def initialize
    super({
      :cmd_full 		=> "display fan",
      :cmd_short 		=> "dis fan",
      :author				=> "Elin",
      :description  => "This is H3C Command# display fan",
      :ranking			=> Ranking_1,
      :platform			=> "all",
      :benchmark		=> {
        :fan_status	=> ["=~", /Normal/],
      },
    })
  end

  def parse
    cont = self.context.dup
    useful[:fan] = {}
    case device_template
    when Template_1
      cont.readline_range_loop(/Slot/, /Slot/) do |cont_layer|
        parse_slot(cont_layer)
      end
    when Template_2
      cont.readline_range_loop(/Unit/, /Unit/) do |cont_layer|
        parse_unit(cont_layer)
      end
    end
  end

  # 遇到的有两种情况，开头是Slot 或者是 Unit

  def parse_slot(context)
    fan = {}
    context.readline_match_block(/Slot (?<slot_id>\d+)/) do |block|
      useful[:fan] = {}
      useful[:fan]["slot_#{block[:slot_id]}"] = fan
      block.update(fan, :slot_id)
    end
    context.readline_range_loop(/FAN \d+/, reg_blank) do |cont_layer|
      parse_slot_2(cont_layer, fan)
    end
  end

  def parse_slot_2(context, fan)
    fan_id = nil
    context.readline_match_block(/FAN (?<fan_id>\d+)/) do |block|
      fan_id = block[:fan_id]
      fan["fan_#{fan_id}"] = {}
      block.update(fan["fan_#{fan_id}"], :fan_id)
    end
    context.readline_match_block(/State : (?<fan_status>.*)/) do |block|
      block.warning_serious(fan["fan_#{fan_id}"], :fan_status, self)
    end
  end

  def parse_unit(context)
    fan = {}
    context.readline_match_block(/Unit (?<slot_id>\d+)/) do |block|
      useful[:fan]["slot_#{block[:slot_id]}"] = fan
      block.update(fan, :slot_id)
    end
    context.readline_match_block_loop(/Fan (?<fan_id>\d+) State: (?<fan_status>.*)/, reg_blank) do |block|
      fan_id = block[:fan_id]
      fan["fan_#{fan_id}"] = {}
      block.update(fan["fan_#{fan_id}"], :fan_id)
      block.warning_serious(fan["fan_#{fan_id}"], :fan_status, self)
    end
  end

end



